import methods from "methods";
import Layer from "./Layer.js";
function Route() {
  this.stack = [];
  this.methods = {}; // 用来描述这个route中有什么方法
}
methods.forEach((method) => {
  Route.prototype[method] = function (handler) {
    let layer = new Layer("里层的用户的逻辑不需要这个path", handler);
    layer.method = method;
    this.methods[method] = true; // {get:true}
    this.stack.push(layer);
  };
});

Route.prototype.dispatch = function (req, res, out) {
  let idx = 0;
  const next = (err) => {
    console.log("run");
    if (err) return out(err);
    if (idx === this.stack.length) return out();
    let layer = this.stack[idx++];
    if (req.method.toLowerCase() === layer.method) {
      // 用户绑定的方法
      layer.handle_request(req, res, next);
    } else {
      next();
    }
  };
  next();
};
export default Route;
